package com.mbc.rmt.manager;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRResultSetDataSource;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.util.JRLoader;

import com.mbc.common.CommonConstants;
import com.mbc.common.CommonSingleton;
import com.mbc.common.dao.CommonDAOFactory;
import com.mbc.common.dao.CurrencyRateDAO;
import com.mbc.common.dao.CurrencyRateDetailDAO;
import com.mbc.common.dao.LogDAO;
import com.mbc.common.entities.CurrencyRate;
import com.mbc.common.entities.CurrencyRateDetail;
import com.mbc.common.entities.Log;
import com.mbc.common.exception.DataDuplicationException;
import com.mbc.common.exception.DataNotFoundException;
import com.mbc.common.manager.AbstractMgr;
import com.mbc.common.param.CriteriaParams;
import com.mbc.common.param.CriteriaRMParams;
import com.mbc.common.utils.KeyGenerator;
import com.mbc.rmt.RemittanceConstants.RMTRecordStatus;
import com.mbc.rmt.dao.AddCodeDAO;
import com.mbc.rmt.dao.AddCodeDetailDAO;
import com.mbc.rmt.dao.AgentBalanceDAO;
import com.mbc.rmt.dao.AgentCOADAO;
import com.mbc.rmt.dao.AgentDAO;
import com.mbc.rmt.dao.BankDAO;
import com.mbc.rmt.dao.BranchBalanceDAO;
import com.mbc.rmt.dao.BranchCOADAO;
import com.mbc.rmt.dao.CashReceivedDownloadLogDAO;
import com.mbc.rmt.dao.CashRemittanceByAgentArchiveDAO;
import com.mbc.rmt.dao.CashRemittanceByAgentArchiveDetailDAO;
import com.mbc.rmt.dao.CashRemittanceByAgentDAO;
import com.mbc.rmt.dao.CashRemittanceByAgentDetailDAO;
import com.mbc.rmt.dao.General_COADAO;
import com.mbc.rmt.dao.RemittanceCashReceivedArchiveDAO;
import com.mbc.rmt.dao.RemittanceCashReceivedDAO;
import com.mbc.rmt.dao.RemittanceDAOFactory;
import com.mbc.rmt.data.CashRemittanceByAgentDataList;
import com.mbc.rmt.data.CashRemittanceByAgentNDetailDataList;
import com.mbc.rmt.entities.AddCode;
import com.mbc.rmt.entities.AddCodeDetail;
import com.mbc.rmt.entities.Agent;
import com.mbc.rmt.entities.AgentBalance;
import com.mbc.rmt.entities.AgentCOA;
import com.mbc.rmt.entities.Bank;
import com.mbc.rmt.entities.BranchBalance;
import com.mbc.rmt.entities.BranchCOA;
import com.mbc.rmt.entities.CashReceivedByIndividualToPayAgentRpt;
import com.mbc.rmt.entities.CashRemittanceByAgent;
import com.mbc.rmt.entities.CashRemittanceByAgentArchiveCri;
import com.mbc.rmt.entities.CashRemittanceByAgentDetails;
import com.mbc.rmt.entities.CashRemittanceByAgentRpt;
import com.mbc.rmt.entities.GeneralLedger;
import com.mbc.rmt.entities.General_COA;
import com.mbc.rmt.entities.RemittanceCashReceived;

public class CashRemittanceByAgentMgr extends AbstractMgr {

	private CashRemittanceByAgentDAO crByAgentDAO;
	private CashRemittanceByAgentDetailDAO crByAgentDetailDAO; 
	private CashRemittanceByAgentArchiveDAO crByAgentArchiveDAO;
	private CashRemittanceByAgentArchiveDetailDAO crByAgentArchiveDetailDAO;
	private CurrencyRateDetailDAO rateDetailDAO;
	private AgentBalanceDAO agentbalanceDAO;
	private BranchBalanceDAO branchBalanceDAO;
	private RemittanceCashReceivedArchiveDAO rcrArchiveDAO;
	private RemittanceCashReceivedDAO rcrDAO;
	
	private BranchCOADAO branchCOADAO;
	private CurrencyRateDAO currencyRateDAO;
	private AgentCOADAO agentCOADAO;
	private General_COADAO generalCOADAO;
	private AgentDAO agentDAO;
	private BankDAO bankDAO;
	
	private LogDAO logDAO;
	
	public static enum GL_STATUS { CREATE, DELETE }
	
	public CashRemittanceByAgentMgr() {
	}

	public CashRemittanceByAgentMgr(String sessionId) {
		this.sessionId = sessionId;
	}

	public String create(CashRemittanceByAgent entity) throws Exception {
		openConnection();
		try {
			beginTransaction();
			crByAgentDAO = RemittanceDAOFactory.getCashRemittanceByAgentDAO(getConnection());
			rateDetailDAO = CommonDAOFactory.getCurrencyRateDetailDAO(dbManager.getConnection());
			agentbalanceDAO = RemittanceDAOFactory.getAgentBalanceDAO(dbManager.getConnection());
			branchBalanceDAO = RemittanceDAOFactory.getBranchBalanceDAO(dbManager.getConnection());
			logDAO = CommonDAOFactory.getLogDAO(getConnection());
			Log l_logentity = new Log();
			String voucher = "";
			CashRemittanceByAgent l_info = new CashRemittanceByAgent();
			l_info = crByAgentDAO.find(entity.getReceiptId());
			if(l_info != null){
				throw new Exception("Invoice ID already exists!");
			} else {
				entity.setReceiptId(KeyGenerator.generateKey(getConnection()));
				
				if(entity.getTransactionType()==1)
					voucher = RemittanceSerialMgr.getFormattedSerial(RemittanceSerialMgr.CASH_REMITTANCE_BY_AGENT,entity.getReceivedDate(),entity.getDefaultBranchId(),getConnection());
				else
					voucher = RemittanceSerialMgr.getFormattedSerial(RemittanceSerialMgr.CASH_RECEIVED_BY_INDIVIDUAL_TO_PAY_AGENT,entity.getReceivedDate(),entity.getDefaultBranchId(),getConnection());
				
				entity.setReceiptNo(voucher);
				gotoGL(entity, GL_STATUS.CREATE);
				crByAgentDAO.create(entity);
				
				l_logentity.setTablename("CashRemittanceByAgent");
				l_logentity.setLogkey(KeyGenerator.generateKey(getConnection()));
				l_logentity.setKey(entity.getReceiptId());
				l_logentity.setUserid(entity.getCreatedBy());
				l_logentity.setLogstatus(Log.__SAVE_DATA);
				logDAO.getData(l_logentity);
				
				crByAgentDetailDAO = RemittanceDAOFactory.getCashRemittanceByAgentDetailDAO(getConnection());
				for (CashRemittanceByAgentDetails l_detail : entity.getCrByAgentDetailList()) {
					
					if(!l_detail.getCode().equals("") && l_detail.getAddcodeId() == 0){
						AddCodeDAO l_AddCodeDAO = RemittanceDAOFactory.getAddCodeDAO(getConnection());
						AddCode l_AddCode = l_AddCodeDAO.findByCode(l_detail.getCode(), entity.getDefaultBranchId());
						if(l_AddCode == null)
							throw new DataNotFoundException("Add Code does not exist.", AddCode.class);
						l_detail.setAddcodeId(l_AddCode.getAddCodeId());
					} else if(l_detail.getCode().equals("")){
						AddCodeDAO l_AddCodeDAO = RemittanceDAOFactory.getAddCodeDAO(getConnection());
						AddCode l_AddCode = l_AddCodeDAO.findByCode("_", entity.getDefaultBranchId());
						if(l_AddCode == null)
							throw new DataNotFoundException("Add Code does not exist for common\"_\".", AddCode.class);
						l_detail.setAddcodeId(l_AddCode.getAddCodeId());
				
						l_AddCode.setCode("_");
						l_AddCode.setSender(l_detail.getSender());
						l_AddCode.setBranchId(entity.getDefaultBranchId());
						l_AddCodeDAO.update(l_AddCode);
						
						l_logentity.setTablename("AddCode");
						l_logentity.setLogkey(KeyGenerator.generateKey(getConnection()));
						l_logentity.setKey(l_AddCode.getAddCodeId());
						l_logentity.setUserid(l_AddCode.getUserid());
						l_logentity.setLogstatus(Log.__UPDATE_DATA);
						logDAO.getData(l_logentity);
						
						AddCodeDetailDAO l_AddCodeDetailDAO = RemittanceDAOFactory.getAddCodeDetailDAO(getConnection());
						AddCodeDetail l_AddCodeDetail = l_AddCodeDetailDAO.findLastByAddCodeId(l_AddCode.getAddCodeId());
			            if( l_AddCode.getAddCodeId() == l_AddCodeDetail.getAddCodeId()){
						   l_AddCodeDetail.setReceiver(l_detail.getReceiver());
						   l_AddCodeDetail.setNRC(l_detail.getNrc());
						   l_AddCodeDetail.setAddress(l_detail.getAddress());  
						   l_AddCodeDetailDAO.update(l_AddCodeDetail);
						   
						   l_logentity.setTablename("AddCodeDetail");
							l_logentity.setLogkey(KeyGenerator.generateKey(getConnection()));
							l_logentity.setKey(l_AddCodeDetail.getAddCodeDetailId());
							logDAO.getData(l_logentity);
			            }
					  
					}
					
					   CurrencyRateDetail l_currencyrateDetail = new CurrencyRateDetail();
						if( l_detail.getRate() != 1){
						l_currencyrateDetail = rateDetailDAO.findLatest(l_detail.getRateId());
						if(l_currencyrateDetail.getRate() != l_detail.getRate()){
							
							l_currencyrateDetail.setCurrencyRateDetailId(KeyGenerator.generateKey(getConnection()));
							l_currencyrateDetail.setCurrencyRateId(l_detail.getRateId());
						    l_currencyrateDetail.setRate(l_detail.getRate());
						    l_currencyrateDetail.seteDateTime(entity.getReceivedDate());
						    rateDetailDAO.create(l_currencyrateDetail);
						    
						    l_logentity.setTablename("CurrencyRateDetail");
						    l_logentity.setLogkey(KeyGenerator.generateKey(getConnection()));
						    l_logentity.setKey(l_currencyrateDetail.getCurrencyRateDetailId());
						    l_logentity.setLogstatus(Log.__SAVE_DATA);
						    logDAO.getData(l_logentity);
							
					     }
				      }
				   
					l_detail.setReceiptDetailId(KeyGenerator.generateKey(getConnection()));
					l_detail.setReceiptId(entity.getReceiptId());
					
					crByAgentDetailDAO.create(l_detail);
					
					l_logentity.setTablename("CashRemittanceByAgentDetails");
					l_logentity.setLogkey(KeyGenerator.generateKey(getConnection()));
					l_logentity.setKey(l_detail.getReceiptDetailId());
					l_logentity.setLogstatus(Log.__SAVE_DATA);
					logDAO.getData(l_logentity);
				}
				
				if(entity.getTransactionType() == 0){
					
					AgentBalance l_AgentBalance = new AgentBalance();
					l_AgentBalance.setAgent_id(entity.getCrByAgentDetailList().get(0).getToAgentId());
					l_AgentBalance.setCurrencyid(entity.getPaymentCurrencyId());
					l_AgentBalance = agentbalanceDAO.getInfo(l_AgentBalance);
               
					if(l_AgentBalance == null)
					{
						AgentBalance l_MAgentBalance = new AgentBalance();
						l_MAgentBalance.setAgentbalance_id(KeyGenerator.generateKey(getConnection()));
						l_MAgentBalance.setAgent_id(entity.getCrByAgentDetailList().get(0).getToAgentId());
						l_MAgentBalance.setCurrencyid(entity.getPaymentCurrencyId());
						l_MAgentBalance.setCurrentbalance(-entity.getTotalPaymentAmt());
						agentbalanceDAO.create(l_MAgentBalance);
					}
					else
					{
						l_AgentBalance.setCurrentbalance(l_AgentBalance.getCurrentbalance()-entity.getTotalPaymentAmt());
						agentbalanceDAO.update(l_AgentBalance);
					}
				}
				else{
					//Header
					AgentBalance l_AgentBalance = new AgentBalance();
					l_AgentBalance.setAgent_id(entity.getFromAgentId());
					l_AgentBalance.setCurrencyid(entity.getReceivedCurrencyId());
					l_AgentBalance = agentbalanceDAO.getInfo(l_AgentBalance);
               
					if(l_AgentBalance == null)
					{
						AgentBalance l_HAgentBalance = new AgentBalance();
						l_HAgentBalance.setAgentbalance_id(KeyGenerator.generateKey(getConnection()));
						l_HAgentBalance.setAgent_id(entity.getFromAgentId());
						l_HAgentBalance.setCurrencyid(entity.getReceivedCurrencyId());
						l_HAgentBalance.setCurrentbalance(entity.getBalance());
						agentbalanceDAO.create(l_HAgentBalance);
					}
					else
					{
						l_AgentBalance.setCurrentbalance(l_AgentBalance.getCurrentbalance()+entity.getBalance());
						agentbalanceDAO.update(l_AgentBalance);
					}
			//Detail  
				for (CashRemittanceByAgentDetails l_Adetail : entity.getCrByAgentDetailList()) {
			
				if(l_Adetail.getToAgentId() !=null)
				{
					AgentBalance l_DAgentBalance = new AgentBalance();
					l_DAgentBalance.setAgent_id(l_Adetail.getToAgentId());
					l_DAgentBalance.setCurrencyid(entity.getPaymentCurrencyId());
					l_DAgentBalance = agentbalanceDAO.getInfo(l_DAgentBalance);
               
					if(l_DAgentBalance == null)
					{
						AgentBalance l_NAgentBalance = new AgentBalance();
						l_NAgentBalance.setAgentbalance_id(KeyGenerator.generateKey(getConnection()));
						l_NAgentBalance.setAgent_id(l_Adetail.getToAgentId());
						l_NAgentBalance.setCurrencyid(entity.getPaymentCurrencyId());
						l_NAgentBalance.setCurrentbalance(-l_Adetail.getPaymentAmt());
						agentbalanceDAO.create(l_NAgentBalance);
					}
					else
					{
						l_DAgentBalance.setCurrentbalance(l_DAgentBalance.getCurrentbalance()-l_Adetail.getPaymentAmt());
						agentbalanceDAO.update(l_DAgentBalance);
					}
				}
				else
				{
					BranchBalance branchBalance = new BranchBalance();
					branchBalance.setBranch_id(l_Adetail.getToBranchId());
					branchBalance.setCurrencyid(entity.getPaymentCurrencyId());
					branchBalance = branchBalanceDAO.getInfo(branchBalance);
               
					if(branchBalance == null)
					{
						BranchBalance bBalance = new BranchBalance();
						bBalance.setBranchbalance_id(KeyGenerator.generateKey(getConnection()));
						bBalance.setBranch_id(l_Adetail.getToBranchId());
						bBalance.setCurrencyid(entity.getPaymentCurrencyId());
						bBalance.setCurrentbalance(-l_Adetail.getPaymentAmt());
						branchBalanceDAO.create(bBalance);
					}
					else
					{
						branchBalance.setCurrentbalance(branchBalance.getCurrentbalance()-l_Adetail.getPaymentAmt());
						branchBalanceDAO.update(branchBalance);
					}
				  }
				 }
				}
			}
			commintTransaction();
			return voucher;
		} catch (SQLException e) {
			rollbackTransaction();
			if (DataDuplicationException.UNIQUE_KEY_ERROR_CODE == e
					.getErrorCode())
				throw new DataDuplicationException(e.getMessage());
			    rollbackTransaction();
		   throw e;
		} finally {
			closeConnection();
		}
	}

	public void createDetail(CashRemittanceByAgentDetails entity)
			throws Exception {
		openConnection();
		try {
			crByAgentDetailDAO = RemittanceDAOFactory.getCashRemittanceByAgentDetailDAO(getConnection());
			entity.setReceiptDetailId(KeyGenerator.generateKey(getConnection()));
			crByAgentDetailDAO.create(entity);
		} catch (Exception e) {
			throw e;
		} finally {
			closeConnection();
		}
	}

	public void update(CashRemittanceByAgent entity) throws Exception {
		openConnection();
		try {
			beginTransaction();
			crByAgentDAO = RemittanceDAOFactory.getCashRemittanceByAgentDAO(getConnection());
			agentbalanceDAO = RemittanceDAOFactory.getAgentBalanceDAO(dbManager.getConnection());
			branchBalanceDAO = RemittanceDAOFactory.getBranchBalanceDAO(dbManager.getConnection());
			
		    if( entity.getTransactionType() == 0){ //Individual
				
				AgentBalance l_UAgentBalance = new AgentBalance();
				double amt=0;
				double tempamt=0;
				long agentid=0;
				long tempagentid=0;
				long currencyid =0;
				long tempcurrencyid=0;
				
				amt=entity.getTotalPaymentAmt();
				agentid=entity.getCrByAgentDetailList().get(0).getToAgentId();
				currencyid=entity.getPaymentCurrencyId();
				
				CashRemittanceByAgent tempentity = new CashRemittanceByAgent();
                tempentity = crByAgentDAO.getCRByAgent(entity);
                
                tempamt = tempentity.getPaymentamt();
                tempagentid = tempentity.getToagentid();
                tempcurrencyid = tempentity.getPaymentCurrencyId();
                
                if(tempagentid == agentid && tempcurrencyid == currencyid)
                {
                	l_UAgentBalance.setAgent_id(entity.getCrByAgentDetailList().get(0).getToAgentId());
                	l_UAgentBalance.setCurrencyid(entity.getPaymentCurrencyId());
                	l_UAgentBalance = agentbalanceDAO.getInfo(l_UAgentBalance);
                	l_UAgentBalance.setCurrentbalance((l_UAgentBalance.getCurrentbalance()+tempamt)-amt);
                	agentbalanceDAO.update(l_UAgentBalance);
                }
                else
                {
                	double amount =0;
                	l_UAgentBalance.setAgent_id(tempagentid);
                	l_UAgentBalance.setCurrencyid(tempcurrencyid);
                	l_UAgentBalance = agentbalanceDAO.getInfo(l_UAgentBalance);
                	amount=l_UAgentBalance.getCurrentbalance()+tempamt;
                	l_UAgentBalance.setCurrentbalance(amount);
                	agentbalanceDAO.update(l_UAgentBalance);
                	
                	l_UAgentBalance.setAgent_id(entity.getCrByAgentDetailList().get(0).getToAgentId());
                	l_UAgentBalance.setCurrencyid(entity.getPaymentCurrencyId());
                	l_UAgentBalance = agentbalanceDAO.getInfo(l_UAgentBalance);
                	if(l_UAgentBalance != null)
                	{
                		if(entity.getCrByAgentDetailList().get(0).getToAgentId() == tempagentid && entity.getPaymentCurrencyId() == tempcurrencyid)
                			l_UAgentBalance.setCurrentbalance(amount -amt);
                		else
                			l_UAgentBalance.setCurrentbalance(l_UAgentBalance.getCurrentbalance()-amt);
                		agentbalanceDAO.update(l_UAgentBalance);
                	}
                	else
                	{
                		l_UAgentBalance = new AgentBalance();
                		l_UAgentBalance.setAgentbalance_id(KeyGenerator.generateKey(getConnection()));
                		l_UAgentBalance.setAgent_id(entity.getCrByAgentDetailList().get(0).getToAgentId());
                		l_UAgentBalance.setCurrencyid(entity.getPaymentCurrencyId());
                		l_UAgentBalance.setCurrentbalance(-amt);
                		agentbalanceDAO.create(l_UAgentBalance);
                	}
                }
		     }//End Individual
		else{ // Transaction type 1 (CR By Agent)
			//Header
			 
			AgentBalance l_UAgentBalance = new AgentBalance();
			double amt=0;
			double tempamt=0;
			long agentid=0;
			long tempagentid=0;
			long currencyid =0;
			long tempcurrencyid=0;
			
			amt=entity.getBalance();
			agentid=entity.getFromAgentId();
			currencyid=entity.getReceivedCurrencyId();
			
			CashRemittanceByAgent tempentity = new CashRemittanceByAgent();
            tempentity = crByAgentDAO.getCRByAgent(entity);
            
            tempamt = tempentity.getBalance();
            tempagentid = tempentity.getFromAgentId();
            tempcurrencyid = tempentity.getReceivedCurrencyId();
            
            if(tempagentid == agentid && tempcurrencyid == currencyid)
            {
            	l_UAgentBalance.setAgent_id(entity.getFromAgentId());
            	l_UAgentBalance.setCurrencyid(entity.getReceivedCurrencyId());
            	l_UAgentBalance = agentbalanceDAO.getInfo(l_UAgentBalance);
            	l_UAgentBalance.setCurrentbalance((l_UAgentBalance.getCurrentbalance()-tempamt)+amt);
            	agentbalanceDAO.update(l_UAgentBalance);
            }
            else
            {
            	double amount =0;
            	l_UAgentBalance.setAgent_id(tempagentid);
            	l_UAgentBalance.setCurrencyid(tempcurrencyid);
            	l_UAgentBalance = agentbalanceDAO.getInfo(l_UAgentBalance);
            	amount=l_UAgentBalance.getCurrentbalance()-tempamt;
            	l_UAgentBalance.setCurrentbalance(amount);
            	agentbalanceDAO.update(l_UAgentBalance);
            	
            	l_UAgentBalance.setAgent_id(entity.getFromAgentId());
            	l_UAgentBalance.setCurrencyid(entity.getReceivedCurrencyId());
            	l_UAgentBalance = agentbalanceDAO.getInfo(l_UAgentBalance);
            	if(l_UAgentBalance != null)
            	{
            		if(entity.getFromAgentId() == tempagentid && entity.getReceivedCurrencyId() == tempcurrencyid)
            			l_UAgentBalance.setCurrentbalance(amount +amt);
            		else
            			l_UAgentBalance.setCurrentbalance(l_UAgentBalance.getCurrentbalance()+amt);
            		agentbalanceDAO.update(l_UAgentBalance);
            	}
            	else
            	{
            		l_UAgentBalance = new AgentBalance();
            		l_UAgentBalance.setAgentbalance_id(KeyGenerator.generateKey(getConnection()));
            		l_UAgentBalance.setAgent_id(entity.getFromAgentId());
            		l_UAgentBalance.setCurrencyid(entity.getReceivedCurrencyId());
            		l_UAgentBalance.setCurrentbalance(+amt);
            		agentbalanceDAO.create(l_UAgentBalance);
            	}
            }
              //Detail
              
            for (CashRemittanceByAgentDetails l_Adetail : entity.getCrByAgentDetailList()) {
            	
              if(l_Adetail.getToAgentId() !=null)
              {
            	l_UAgentBalance = new AgentBalance();
      			double toamt=0;
      			double temptoamt=0;
      			long toagentid=0;
      			long temptoagentid=0;
      			long tocurrencyid =0;
      			long temptocurrencyid=0;
      			
      			toamt=l_Adetail.getPaymentAmt();
      			toagentid=l_Adetail.getToAgentId();
      			tocurrencyid=entity.getPaymentCurrencyId();
      			
      			CashRemittanceByAgent tempagententity = new CashRemittanceByAgent();
      			tempagententity = crByAgentDAO.getCRByAgent(entity);
                  
                  temptoamt = tempagententity.getPaymentamt();
                  temptoagentid = tempagententity.getToagentid();
                  temptocurrencyid = tempagententity.getPaymentCurrencyId();
                  
                  if(temptoagentid == toagentid && temptocurrencyid == tocurrencyid)
                  {
                  	l_UAgentBalance.setAgent_id(l_Adetail.getToAgentId());
                  	l_UAgentBalance.setCurrencyid(entity.getPaymentCurrencyId());
                  	l_UAgentBalance = agentbalanceDAO.getInfo(l_UAgentBalance);
                  	l_UAgentBalance.setCurrentbalance((l_UAgentBalance.getCurrentbalance()+temptoamt)-toamt);
                  	agentbalanceDAO.update(l_UAgentBalance);
                  }
                  else
                  {
                  	double amount =0;
                  	l_UAgentBalance.setAgent_id(temptoagentid);
                  	l_UAgentBalance.setCurrencyid(temptocurrencyid);
                  	l_UAgentBalance = agentbalanceDAO.getInfo(l_UAgentBalance);
                  	amount=l_UAgentBalance.getCurrentbalance()+temptoamt;
                  	l_UAgentBalance.setCurrentbalance(amount);
                  	agentbalanceDAO.update(l_UAgentBalance);
                  	
                  	l_UAgentBalance.setAgent_id(l_Adetail.getToAgentId());
                  	l_UAgentBalance.setCurrencyid(entity.getPaymentCurrencyId());
                  	l_UAgentBalance = agentbalanceDAO.getInfo(l_UAgentBalance);
                  	if(l_UAgentBalance != null)
                  	{
                  		if(l_Adetail.getToAgentId() == temptoagentid && entity.getPaymentCurrencyId() == temptocurrencyid)
                  			l_UAgentBalance.setCurrentbalance(amount -toamt);
                  		else
                  			l_UAgentBalance.setCurrentbalance(l_UAgentBalance.getCurrentbalance()-toamt);
                  		agentbalanceDAO.update(l_UAgentBalance);
                  	}
                  	else
                  	{
                  		l_UAgentBalance = new AgentBalance();
                  		l_UAgentBalance.setAgentbalance_id(KeyGenerator.generateKey(getConnection()));
                  		l_UAgentBalance.setAgent_id(l_Adetail.getToAgentId());
                  		l_UAgentBalance.setCurrencyid(entity.getPaymentCurrencyId());
                  		l_UAgentBalance.setCurrentbalance(-toamt);
                  		agentbalanceDAO.create(l_UAgentBalance);
                  	}
                  }
                }
                  else if(l_Adetail.getToBranchId() !=0) //To BranchID
                   {
                  	    BranchBalance balance = new BranchBalance();
            			double bamt=0;
            			double tempbamt=0;
            			long branchid=0;
            			long tempbranchid=0;
            			long bcurrencyid =0;
            			long tempbcurrencyid=0;
            			
            			bamt=l_Adetail.getPaymentAmt();
            			branchid=l_Adetail.getToBranchId();
            			bcurrencyid=entity.getPaymentCurrencyId();
            			
            			CashRemittanceByAgent tempbentity = new CashRemittanceByAgent();
            			tempbentity = crByAgentDAO.getCRByAgent(entity);
                        
            			if(tempbentity.getTobranchid() !=0){
                          tempbamt = tempbentity.getPaymentamt();
                          tempbranchid = tempbentity.getTobranchid();
                          tempbcurrencyid = tempbentity.getPaymentCurrencyId();
                        
                        if(tempbranchid == branchid && tempbcurrencyid == bcurrencyid)
                        {
                        	balance.setBranch_id(l_Adetail.getToBranchId());
                        	balance.setCurrencyid(entity.getPaymentCurrencyId());
                        	balance = branchBalanceDAO.getInfo(balance);
                        	balance.setCurrentbalance((balance.getCurrentbalance()+tempbamt)-bamt);
                        	branchBalanceDAO.update(balance);
                        }
                        else
                        {
                        	double amount =0;
                        	balance.setBranch_id(tempbranchid);
                        	balance.setCurrencyid(tempbcurrencyid);
                        	balance = branchBalanceDAO.getInfo(balance);
                        	amount=balance.getCurrentbalance()+tempbamt;
                        	balance.setCurrentbalance(amount);
                        	branchBalanceDAO.update(balance);
                        	
                        	balance.setBranch_id(l_Adetail.getToBranchId());
                        	balance.setCurrencyid(entity.getPaymentCurrencyId());
                        	balance = branchBalanceDAO.getInfo(balance);
                        	if(balance != null)
                        	{
                        		if(l_Adetail.getToBranchId() == tempbranchid && entity.getPaymentCurrencyId() == tempbcurrencyid)
                        			balance.setCurrentbalance(amount -bamt);
                        		else
                        			balance.setCurrentbalance(balance.getCurrentbalance()+bamt);
                        		branchBalanceDAO.update(balance);
                        	}
                        	else
                        	{
                        		balance = new BranchBalance();
                        		balance.setBranchbalance_id(KeyGenerator.generateKey(getConnection()));
                        		balance.setBranch_id(l_Adetail.getToBranchId());
                        		balance.setCurrencyid(entity.getPaymentCurrencyId());
                        		balance.setCurrentbalance(-bamt);
                        		branchBalanceDAO.create(balance);
                        	}
                        }
            			}
                  }
                }
		}

	    gotoGL(entity, GL_STATUS.CREATE);
	    
		crByAgentDAO.update(entity);
		
		logDAO = CommonDAOFactory.getLogDAO(getConnection());
		Log l_logentity = new Log();
		l_logentity.setTablename("CashRemittanceByAgent");
		l_logentity.setLogkey(KeyGenerator.generateKey(getConnection()));
		l_logentity.setKey(entity.getReceiptId());
		l_logentity.setUserid(entity.getModifiedBy());
		l_logentity.setLogstatus(Log.__UPDATE_DATA);
		logDAO.getData(l_logentity);
		
		crByAgentDetailDAO = RemittanceDAOFactory.getCashRemittanceByAgentDetailDAO(getConnection());
		crByAgentDetailDAO.deleteByReceiptId(entity.getReceiptId());
		for (CashRemittanceByAgentDetails l_detail : entity.getCrByAgentDetailList()) {
			l_detail.setReceiptId(entity.getReceiptId());
			l_detail.setReceiptDetailId(KeyGenerator.generateKey(getConnection()));
			crByAgentDetailDAO.create(l_detail);
			
			l_logentity.setTablename("CashRemittanceByAgentDetails");
			l_logentity.setLogkey(KeyGenerator.generateKey(getConnection()));
			l_logentity.setKey(l_detail.getReceiptDetailId());
			l_logentity.setLogstatus(Log.__UPDATE_DATA);
			logDAO.getData(l_logentity);
			
			rateDetailDAO = CommonDAOFactory.getCurrencyRateDetailDAO(dbManager.getConnection());
			CurrencyRateDetail l_currencyrateDetail = new CurrencyRateDetail();
			if( l_detail.getRate() != 1){
			l_currencyrateDetail = rateDetailDAO.findLatest(l_detail.getRateId());
			if(l_currencyrateDetail.getRate() != l_detail.getRate()){
				l_currencyrateDetail.setCurrencyRateDetailId(KeyGenerator.generateKey(getConnection()));
				l_currencyrateDetail.setCurrencyRateId(l_detail.getRateId());
			    l_currencyrateDetail.setRate(l_detail.getRate());
			    l_currencyrateDetail.seteDateTime(entity.getReceivedDate());
			    rateDetailDAO.create(l_currencyrateDetail);
			    
			    l_logentity.setTablename("CurrencyRateDetail");
			    l_logentity.setLogkey(KeyGenerator.generateKey(getConnection()));
			    l_logentity.setKey(l_currencyrateDetail.getCurrencyRateDetailId());
			    l_logentity.setLogstatus(Log.__SAVE_DATA);
			    logDAO.getData(l_logentity);
			 }
			}
			
		}
			commintTransaction();
		} catch (Exception e) {
			rollbackTransaction();
			throw e;
		} finally {
			closeConnection();
		}
	}

	public void delete(CashRemittanceByAgent l_info) throws Exception {
		openConnection();
		try {
			beginTransaction();
			
			crByAgentDAO = RemittanceDAOFactory
					.getCashRemittanceByAgentDAO(getConnection());
			crByAgentDetailDAO = RemittanceDAOFactory
					.getCashRemittanceByAgentDetailDAO(getConnection());
			crByAgentArchiveDAO = RemittanceDAOFactory.getCashRemittanceByAgentArchiveDAO(getConnection());
			crByAgentArchiveDetailDAO = RemittanceDAOFactory.getCashRemittanceByAgentArchiveDetailDAO(getConnection());
			logDAO = CommonDAOFactory.getLogDAO(getConnection());
			
		    CashRemittanceByAgent l_archive= crByAgentDAO.findById(l_info.getReceiptId());
		    
	    /*l_archive.setReceiptId(l_info.getReceiptId());
	    l_archive.setReceiptNo(l_info.getReceiptNo());
	    l_archive.setCreatedDate(l_info.getCreatedDate());
	    l_archive.setReceivedDate(l_info.getReceivedDate());
	    l_archive.setDefaultBranchId(l_info.getDefaultBranchId());
	    l_archive.setCreatedBy(l_info.getCreatedBy());
	    l_archive.setFromAgentId(l_info.getFromAgentId());
	    l_archive.setPaymentCurrencyId(l_info.getPaymentCurrencyId());
	    l_archive.setReceivedCurrencyId(l_info.getReceivedCurrencyId());
	    l_archive.setRate(l_info.getRate());
	    l_archive.setTotalPaymentAmt(l_info.getTotalPaymentAmt());
	    l_archive.setTotalReceivedAmt(l_info.getTotalReceivedAmt());
	    l_archive.setExtraFees(l_info.getExtraFees());
	    l_archive.setReceived(l_info.getReceived());
	    l_archive.setBankId(l_info.getBankId());*/
		    l_archive.setStatus(CommonConstants.RecordStatus.delete);
		    crByAgentArchiveDAO.create(l_archive);
		    
		    Log l_logentity = new Log();
		    l_logentity.setTablename("CashRemittanceByAgent");
		    l_logentity.setLogkey(KeyGenerator.generateKey(getConnection()));
		    l_logentity.setKey(l_info.getReceiptId());
		    l_logentity.setUserid(l_info.getModifiedBy());
		    l_logentity.setLogstatus(Log.__DELETE_DATA);
		    logDAO.getData(l_logentity);
		    
		    CashRemittanceByAgentDetails l_archiveDetail = new CashRemittanceByAgentDetails();
            for(int i =0 ;i< l_info.getCrByAgentDetailList().size();i++){

            	l_archiveDetail.setReceiptDetailId(l_info.getCrByAgentDetailList().get(i).getReceiptDetailId());
            	l_archiveDetail.setReceiptId(l_info.getCrByAgentDetailList().get(i).getReceiptId());
            	l_archiveDetail.setAddcodeId(l_info.getCrByAgentDetailList().get(i).getAddcodeId());
            	l_archiveDetail.setSender(l_info.getCrByAgentDetailList().get(i).getSender());
            	l_archiveDetail.setReceiver(l_info.getCrByAgentDetailList().get(i).getReceiver());
            	l_archiveDetail.setPaymentAmt(l_info.getCrByAgentDetailList().get(i).getPaymentAmt());
            	l_archiveDetail.setReceivedAmt(l_info.getCrByAgentDetailList().get(i).getReceivedAmt());
            	l_archiveDetail.setExtraFees(l_info.getCrByAgentDetailList().get(i).getExtraFees());
            	l_archiveDetail.setNrc(l_info.getCrByAgentDetailList().get(i).getNrc());
            	l_archiveDetail.setAddress(l_info.getCrByAgentDetailList().get(i).getAddress());
            	l_archiveDetail.setToAgentId(l_info.getCrByAgentDetailList().get(i).getToAgentId());
            	l_archiveDetail.setToBranchId(l_info.getCrByAgentDetailList().get(i).getToBranchId());
            	l_archiveDetail.setStatus(l_info.getStatus());
            	crByAgentArchiveDetailDAO.create(l_archiveDetail);
            	
            	 l_logentity.setTablename("CashRemittanceByAgentDetails");
     		    l_logentity.setLogkey(KeyGenerator.generateKey(getConnection()));
     		    l_logentity.setKey(l_archiveDetail.getReceiptDetailId());
     		    l_logentity.setLogstatus(Log.__DELETE_DATA);
     		    logDAO.getData(l_logentity);
            	
            }
            agentbalanceDAO = RemittanceDAOFactory.getAgentBalanceDAO(dbManager.getConnection());
            branchBalanceDAO = RemittanceDAOFactory.getBranchBalanceDAO(dbManager.getConnection());
          if(l_info.getTransactionType() == 0){
        	  AgentBalance l_AgentBalance = new AgentBalance();
				l_AgentBalance.setAgent_id(l_info.getCrByAgentDetailList().get(0).getToAgentId());
				l_AgentBalance.setCurrencyid(l_info.getPaymentCurrencyId());
				l_AgentBalance = agentbalanceDAO.getInfo(l_AgentBalance);
         
				if(l_AgentBalance != null)
				{
					l_AgentBalance.setCurrentbalance(l_AgentBalance.getCurrentbalance()+l_info.getTotalPaymentAmt());
					agentbalanceDAO.update(l_AgentBalance);
				}
			}
          else{
        	  //Header
        	  AgentBalance l_AgentBalance = new AgentBalance();
				l_AgentBalance.setAgent_id(l_info.getFromAgentId());
				l_AgentBalance.setCurrencyid(l_info.getReceivedCurrencyId());
				l_AgentBalance = agentbalanceDAO.getInfo(l_AgentBalance);
       
				if(l_AgentBalance != null)
				{
					l_AgentBalance.setCurrentbalance(l_AgentBalance.getCurrentbalance()-l_info.getBalance());
					agentbalanceDAO.update(l_AgentBalance);
				}
				//Detail
				for (CashRemittanceByAgentDetails l_Adetail : l_info.getCrByAgentDetailList()) {
					if(l_Adetail.getToAgentId() != null){
						AgentBalance l_DAgentBalance = new AgentBalance();
						l_DAgentBalance.setAgent_id(l_Adetail.getToAgentId());
						l_DAgentBalance.setCurrencyid(l_info.getPaymentCurrencyId());
						l_DAgentBalance = agentbalanceDAO.getInfo(l_DAgentBalance);
		       
						if(l_DAgentBalance != null)
						{
							l_DAgentBalance.setCurrentbalance(l_DAgentBalance.getCurrentbalance()+l_Adetail.getPaymentAmt());
							agentbalanceDAO.update(l_DAgentBalance);
						}
					}
					else{
						BranchBalance l_DBranchBalance = new BranchBalance();
						l_DBranchBalance.setBranch_id(l_Adetail.getToBranchId());
						l_DBranchBalance.setCurrencyid(l_info.getPaymentCurrencyId());
						l_DBranchBalance = branchBalanceDAO.getInfo(l_DBranchBalance);
						if(l_DBranchBalance != null){
							l_DBranchBalance.setCurrentbalance(l_DBranchBalance.getCurrentbalance()+l_Adetail.getPaymentAmt());
							branchBalanceDAO.update(l_DBranchBalance);
						}
					}
						
				}
          }
          
            gotoGL(l_info, GL_STATUS.DELETE);
			
			crByAgentDetailDAO.deleteByReceiptId(l_info.getReceiptId());
			crByAgentDAO.delete(l_info.getReceiptId());
			
			commintTransaction();
		} catch (Exception e) {
			rollbackTransaction();
			throw e;
		} finally {
			closeConnection();
		}
	}
	public void confirm(ArrayList<CashRemittanceByAgentDetails> cragentList,String status)throws Exception {
		openConnection();
		try{
			beginTransaction();
			crByAgentDAO = RemittanceDAOFactory
					.getCashRemittanceByAgentDAO(getConnection());
			crByAgentDetailDAO = RemittanceDAOFactory
					.getCashRemittanceByAgentDetailDAO(getConnection());
			crByAgentArchiveDAO = RemittanceDAOFactory.getCashRemittanceByAgentArchiveDAO(getConnection());
			crByAgentArchiveDetailDAO = RemittanceDAOFactory.getCashRemittanceByAgentArchiveDetailDAO(getConnection());
			rcrDAO = RemittanceDAOFactory.getRemittanceCashReceivedDAO(getConnection());
			rcrArchiveDAO = RemittanceDAOFactory.getRemittanceCashReceivedArchiveDAO(getConnection());
			
			if(status.equalsIgnoreCase("agent")){
			for(int i = 0; i < cragentList.size(); i++) {
				CashRemittanceByAgent l_archive = crByAgentDAO.findById(cragentList.get(i).getReceiptId());
				l_archive.setStatus(RMTRecordStatus.confirmed);
				
				if(crByAgentArchiveDAO.find(l_archive.getReceiptId()) == null)
					crByAgentArchiveDAO.create(l_archive);
				
				CashRemittanceByAgentDetails l_detailConfirm = crByAgentDetailDAO.find(cragentList.get(i).getReceiptDetailId());
				
				if(l_detailConfirm != null) {
					crByAgentArchiveDetailDAO.create(l_detailConfirm);
					crByAgentDetailDAO.delete(l_detailConfirm.getReceiptDetailId());
				}
				
				/*ArrayList<CashRemittanceByAgentDetails> l_archiveDetail = crByAgentDetailDAO.findCRByAgentDetails(cragentList.get(i).getReceiptId());
				for(int j = 0; j < l_archiveDetail.size(); j++) {
					crByAgentArchiveDetailDAO.create(l_archiveDetail.get(j));
					crByAgentDetailDAO.delete(l_archiveDetail.get(j).getReceiptDetailId());
				}*/
				
				if(crByAgentDetailDAO.findCRByAgentDetails(cragentList.get(i).getReceiptId()).size() == 0)
					crByAgentDAO.delete(cragentList.get(i).getReceiptId());
			}
			}
			else{
				for(int i=0;i<cragentList.size();i++){
					CashRemittanceByAgent l_archive = crByAgentDAO.findById(cragentList.get(i).getReceiptId());	
					if(l_archive==null){
						RemittanceCashReceived l_rcr = rcrDAO.find(cragentList.get(i).getReceiptId());
						l_rcr.setStatus(RMTRecordStatus.confirmed);
						if(rcrArchiveDAO.find(l_rcr.getReceivedId())==null){
							rcrArchiveDAO.create(l_rcr);
							rcrDAO.delete(l_rcr.getReceivedId());
						}
					
					}else{
						l_archive.setStatus(RMTRecordStatus.confirmed);
						if(crByAgentArchiveDAO.find(l_archive.getReceiptId()) == null)
							crByAgentArchiveDAO.create(l_archive);
						
						CashRemittanceByAgentDetails l_detailConfirm = crByAgentDetailDAO.find(cragentList.get(i).getReceiptDetailId());
						
						if(l_detailConfirm != null) {
							crByAgentArchiveDetailDAO.create(l_detailConfirm);
							crByAgentDetailDAO.delete(l_detailConfirm.getReceiptDetailId());
						}
						if(crByAgentDetailDAO.findCRByAgentDetails(cragentList.get(i).getReceiptId()).size() == 0)
							crByAgentDAO.delete(cragentList.get(i).getReceiptId());
					}
					
				}
			}
				
			commintTransaction();
		}catch(Exception e){
			rollbackTransaction();
			throw e;
		}finally{
			closeConnection();
		}
	}
	public CashRemittanceByAgent find(long key)throws Exception{
		openConnection();
		try {
			crByAgentDAO = RemittanceDAOFactory.getCashRemittanceByAgentDAO(getConnection());
			return crByAgentDAO.find(key);
		} catch (Exception e) {
			throw e;
		} finally {
			closeConnection();
		}
	}

	public CashRemittanceByAgentDataList findCRByAgent(CriteriaRMParams cri)
			throws Exception {
		openConnection();
		try {
			crByAgentDAO = RemittanceDAOFactory.getCashRemittanceByAgentDAO(getConnection());
			crByAgentDetailDAO =RemittanceDAOFactory.getCashRemittanceByAgentDetailDAO(getConnection());
			CashRemittanceByAgentDataList l_dataList = new CashRemittanceByAgentDataList();
			if(cri.isIs_All() == true && cri.isIs_Rpt() == false)
			   l_dataList =  crByAgentDAO.findCRByAgent(cri);
			else if(cri.isIs_All() == false && cri.isIs_Rpt() == false){
				l_dataList =  crByAgentDAO.findCRByAgent(cri);
				for(int i = 0; i < l_dataList.getEntityList().size(); i++) {
					ArrayList<CashRemittanceByAgentDetails> l_detail = new ArrayList<CashRemittanceByAgentDetails>();
					l_detail=crByAgentDetailDAO.findCRByAgentDetails(l_dataList.getEntityList().get(i).getReceiptId());
					l_dataList.getEntityList().get(i).setCrByAgentDetailList(l_detail);
				}
			}
			else if(cri.isIs_All() == true && cri.isIs_Rpt() == true)
				l_dataList = crByAgentDAO.findAllCRByAgent(cri);
			else if(cri.isIs_All() == false && cri.isIs_Rpt() == true)
				l_dataList = crByAgentDAO.findAllCRByAgent(cri);
			
			return l_dataList;
		} catch (Exception e) {
			throw e;
		} finally {
			closeConnection();
		}
	}
	
	public CashRemittanceByAgentDataList findCRByAgentArchive(CriteriaParams cri)throws Exception{
		openConnection();
		try {

			CashRemittanceByAgentArchiveDAO l_archiveDAO = RemittanceDAOFactory.getCashRemittanceByAgentArchiveDAO(getConnection());
				return l_archiveDAO.findCRByAgentArchive(cri);
		} catch (Exception e) {
			throw e;
		} finally {
			closeConnection();
		}
	}
	
	public List<CashRemittanceByAgent> findByDateRange(Date fromDate, Date toDate, long branchId) throws Exception{
		openConnection();
		try {
			CashRemittanceByAgentArchiveDAO rmtArchiveDAO = RemittanceDAOFactory.getCashRemittanceByAgentArchiveDAO(getConnection());
			return rmtArchiveDAO.findByDateRange(fromDate, toDate, branchId);
		} catch (Exception e) {
			throw e;
		} finally {
			closeConnection();
		}
	}
	public void updateDownloadStats(long batchId)throws Exception{
		openConnection();
		try {
			CashReceivedDownloadLogDAO l_logDAO = RemittanceDAOFactory.getCashReceivedDownloadLogDAO(getConnection());
			l_logDAO.updateDownloadStatus(batchId);
		} catch (Exception e) {
			throw e;
		} finally {
			closeConnection();
		}
	}
	public CashRemittanceByAgent find(String receiptNo, boolean status)throws Exception{
		openConnection();
		try {
			crByAgentDAO = RemittanceDAOFactory.getCashRemittanceByAgentDAO(getConnection());
			CashRemittanceByAgent l_agent = new CashRemittanceByAgent();
			if( status == true)
				l_agent= crByAgentDAO.findByNo(receiptNo,true);
			else 
				l_agent= crByAgentDAO.findByNo(receiptNo, false);
			ArrayList<CashRemittanceByAgentDetails> l_detail = new ArrayList<CashRemittanceByAgentDetails>();
			crByAgentDetailDAO =RemittanceDAOFactory.getCashRemittanceByAgentDetailDAO(getConnection());
		    l_detail=crByAgentDetailDAO.findCRByAgentDetails(l_agent.getReceiptId());
		    l_agent.setCrByAgentDetailList(l_detail);
			return l_agent;
		} catch (Exception e) {
			throw e;
		} finally {
			closeConnection();
		}
	}
	
	public CashRemittanceByAgent findCRByAgentDetails(
			long ReceiptId,int status) throws Exception {
		openConnection();
		try {
			/*crByAgentDAO = RemittanceDAOFactory
					.getCashRemittanceByAgentDAO(getConnection());
			CashRemittanceByAgent l_agent= crByAgentDAO.findById(ReceiptId);*/
			CashRemittanceByAgent l_agent = new CashRemittanceByAgent();
			if( ReceiptId != 0){
				if(status == 0){
				   crByAgentDetailDAO =RemittanceDAOFactory.getCashRemittanceByAgentDetailDAO(getConnection());
				   l_agent.setCrByAgentDetailList(crByAgentDetailDAO.findCRByAgentDetails(ReceiptId));
				}
				else{
					crByAgentArchiveDetailDAO=RemittanceDAOFactory.getCashRemittanceByAgentArchiveDetailDAO(getConnection());
					l_agent.setCrByAgentDetailList(crByAgentArchiveDetailDAO.findCRByAgentDetailsArchive(ReceiptId));
				}
			}
			return l_agent;
		} catch (Exception e) {
			throw e;
		} finally {
			closeConnection();
		}
	}

	public CashRemittanceByAgentNDetailDataList findforConfirm(CashRemittanceByAgentArchiveCri archivecri,String status)
			throws Exception {
		openConnection();
		try {
			crByAgentDetailDAO = RemittanceDAOFactory.getCashRemittanceByAgentDetailDAO(getConnection());
			return crByAgentDetailDAO.findforConfirm(archivecri,status);
		} catch (Exception e) {
			throw e;
		} finally {
			closeConnection();
		}
	}
	public CashRemittanceByAgent findforConfirmDetail(long key)throws Exception{
		openConnection();
		try{
			crByAgentDAO = RemittanceDAOFactory.getCashRemittanceByAgentDAO(getConnection());
			CashRemittanceByAgent l_agent = crByAgentDAO.findById(key);
			if(l_agent != null){
				crByAgentDetailDAO = RemittanceDAOFactory.getCashRemittanceByAgentDetailDAO(getConnection());
				l_agent.setCrByAgentDetailList(crByAgentDetailDAO.findforConfirmDetail(key));
						
			}
			return l_agent;
		} catch (Exception e){
			throw e;
		}finally{
			closeConnection();
		}		
	}	

	//Cash Received By Individual Report
	
	public JasperPrint prepareCRByIndividualToPayAgentReport(CashReceivedByIndividualToPayAgentRpt info) throws Exception {
		openConnection();
		JasperPrint l_jsPrint = null;
		try {
			
			crByAgentDAO = RemittanceDAOFactory.getCashRemittanceByAgentDAO(getConnection());
			JRResultSetDataSource l_jrRS = new JRResultSetDataSource(crByAgentDAO.prepareCRByIndividualToPayAgentReport(info));
			JasperReport l_jsRpt = (JasperReport) JRLoader.loadObjectFromFile(CommonSingleton.ABSOLUTE_PATH + "reports/CashReceivedByIndividualToPayAgentRpt.jasper");
			HashMap<String, Object> l_params = new HashMap<String, Object>();
			l_params.put("company", "MBC");
			l_jsPrint = JasperFillManager.fillReport(l_jsRpt, l_params, l_jrRS);
			
		} catch (JRException je) {
			throw je;
		} catch (Exception e) {
			throw e;
		} finally {
			closeConnection();
		}

		return l_jsPrint;

	}
	
	//Cash Remittance By Agent Report
	
		public JasperPrint prepareCRByAgentReport(CashRemittanceByAgentRpt info) throws Exception {
			openConnection();
			JasperPrint l_jsPrint = null;
			try {
				
				crByAgentDAO = RemittanceDAOFactory.getCashRemittanceByAgentDAO(getConnection());
				JRResultSetDataSource l_jrRS = new JRResultSetDataSource(crByAgentDAO.prepareCRByAgentReport(info));
				JasperReport l_jsRpt=null;
				if(info.getStatus().equalsIgnoreCase("summary"))
					l_jsRpt = (JasperReport) JRLoader.loadObjectFromFile(CommonSingleton.ABSOLUTE_PATH + "reports/CashRemittanceByAgentSummaryRpt.jasper");
				else 
					l_jsRpt = (JasperReport) JRLoader.loadObjectFromFile(CommonSingleton.ABSOLUTE_PATH + "reports/CashRemittanceByAgentDetailsRpt.jasper");
				HashMap<String, Object> l_params = new HashMap<String, Object>();
				l_params.put("company", "MBC");
				l_jsPrint = JasperFillManager.fillReport(l_jsRpt, l_params, l_jrRS);
				
			} catch (JRException je) {
				throw je;
			} catch (Exception e) {
				throw e;
			} finally {
				closeConnection();
			}

			return l_jsPrint;

		}
		
	public void gotoGL(CashRemittanceByAgent l_entity, GL_STATUS status) throws Exception {
			
			currencyRateDAO = CommonDAOFactory.getCurrencyRateDAO(getConnection());
			branchCOADAO = RemittanceDAOFactory.getBranchCOADAO(getConnection());
			agentCOADAO = RemittanceDAOFactory.getAgentCOADAO(getConnection());
			generalCOADAO = RemittanceDAOFactory.getGeneral_COADAO(getConnection());
			agentDAO = RemittanceDAOFactory.getAgentDAO(getConnection());
			bankDAO = RemittanceDAOFactory.getBankDAO(getConnection());
			
		  if(l_entity.getTransactionType() == 1){ //Cash Remittance By Agent
			  GeneralLedgerMgr l_glmgr = new GeneralLedgerMgr(this.sessionId);
			  l_glmgr.deleteGL(l_entity.getReceiptNo() + "-BA");
			  
			if(status == GL_STATUS.CREATE) {
				GeneralLedger _glHeader = new GeneralLedger();
				_glHeader.setGlKey(l_glmgr.getGLKey());
				_glHeader.setSlip_ID(l_entity.getReceiptNo() + "-BA");
				_glHeader.setSlipDate(l_entity.getReceivedDate());
				_glHeader.setSystemDate(new Date());
				_glHeader.setStatus("JH");
				_glHeader.setSrNo(1);
				_glHeader.setOffice_ID(l_entity.getDefaultBranchId());
				
			CurrencyRate l_currencyrate = currencyRateDAO.getCurrencyRateFromSourcetoHome(l_entity.getReceivedCurrencyId(), l_entity.getDefaultBranchId());
			if(l_entity.getFromAgentId()!=0){
				Agent agent = agentDAO.findById(l_entity.getFromAgentId());
				if(agent != null){
					AgentCOA agentcoa = agentCOADAO.find(agent.getAccountset_id());
					if(agentcoa !=null){
						GeneralLedger _glagentHeader = new GeneralLedger();
						_glagentHeader.setGlKey(l_glmgr.getGLKey());
						_glagentHeader.setSlip_ID(l_entity.getReceiptNo() + "-BA");
						_glagentHeader.setSlipDate(l_entity.getReceivedDate());
						_glagentHeader.setSystemDate(new Date());
						_glagentHeader.setStatus("JBA");
						_glagentHeader.setSrNo(1);
						_glagentHeader.setOffice_ID(l_entity.getDefaultBranchId());
						_glagentHeader.setCoaKey(agentcoa.getAccpayable());
						_glagentHeader.setDepartmentKey(agentcoa.getAccpayable_dept());
						_glagentHeader.setCurrencyKey(l_entity.getReceivedCurrencyId());
						_glagentHeader.setCurrencyRateKey(l_currencyrate.getCurrencyRateId());
						_glagentHeader.setExchangeRate(l_currencyrate.getRate());
						_glagentHeader.setSourceAmount(l_entity.getBalance());
						
						if(l_currencyrate.getRoper().equals("M"))
							_glagentHeader.setHomeAmount((l_entity.getBalance() * l_currencyrate.getRate()));
						else
							_glagentHeader.setHomeAmount((l_entity.getBalance() / l_currencyrate.getRate()));
						
						
						l_glmgr.createGL(_glagentHeader);
						
					}else
						throw new DataNotFoundException("Agent COA doesn't exist", AgentCOA.class);
					
				}
				else
					throw new DataNotFoundException("Agent doesn't exist", Agent.class);
			}
			if(l_entity.getReceived()!=0){
				if(l_entity.getBankId()!=null){
					Bank l_bank = bankDAO.find(l_entity.getBankId());
					if(l_bank != null){
						GeneralLedger _glHeaderBank = new GeneralLedger();
						_glHeaderBank.setGlKey(l_glmgr.getGLKey());
						_glHeaderBank.setSlip_ID(l_entity.getReceiptNo() + "-BA");
						_glHeaderBank.setSlipDate(l_entity.getReceivedDate());
						_glHeaderBank.setSystemDate(new Date());
						_glHeaderBank.setStatus("JBA");
						_glHeaderBank.setSrNo(2);
						_glHeaderBank.setOffice_ID(l_entity.getDefaultBranchId());
						_glHeaderBank.setCoaKey(l_bank.getCoaid());
						_glHeaderBank.setDepartmentKey(l_bank.getDepartmentid());
						_glHeaderBank.setCurrencyKey(l_entity.getReceivedCurrencyId());
						_glHeaderBank.setCurrencyRateKey(l_currencyrate.getCurrencyRateId());
						_glHeaderBank.setExchangeRate(l_currencyrate.getRate());
						_glHeaderBank.setSourceAmount(l_entity.getReceived());
						
						if(l_currencyrate.getRoper().equals("M"))
							_glHeaderBank.setHomeAmount((l_entity.getReceived() * l_currencyrate.getRate()));
						else
							_glHeaderBank.setHomeAmount((l_entity.getReceived() / l_currencyrate.getRate()));
						
						l_glmgr.createGL(_glHeaderBank);
					}
				}else{
					General_COA l_generalCOA = generalCOADAO.find(l_entity.getDefaultBranchId());
					
					if(l_generalCOA == null)
						throw new DataNotFoundException("Plz fill general coa" , General_COA.class);
					
					GeneralLedger _glHeaderGeneral = new GeneralLedger();
					_glHeaderGeneral.setGlKey(l_glmgr.getGLKey());
					_glHeaderGeneral.setSlip_ID(l_entity.getReceiptNo() + "-BA");
					_glHeaderGeneral.setSlipDate(l_entity.getReceivedDate());
					_glHeaderGeneral.setSystemDate(new Date());
					_glHeaderGeneral.setStatus("JBA");
					_glHeaderGeneral.setSrNo(2);
					_glHeaderGeneral.setOffice_ID(l_entity.getDefaultBranchId());
					_glHeaderGeneral.setCoaKey(l_generalCOA.getCashAccountID());
					_glHeaderGeneral.setDepartmentKey(l_generalCOA.getCashDepartmentID());
					_glHeaderGeneral.setCurrencyKey(l_entity.getReceivedCurrencyId());
					_glHeaderGeneral.setCurrencyRateKey(l_currencyrate.getCurrencyRateId());
					_glHeaderGeneral.setExchangeRate(l_currencyrate.getRate());
					_glHeaderGeneral.setSourceAmount(l_entity.getReceived());
					
					if(l_currencyrate.getRoper().equals("M"))
						_glHeaderGeneral.setHomeAmount(l_entity.getReceived() * l_currencyrate.getRate());
					else
						_glHeaderGeneral.setHomeAmount(l_entity.getReceived() / l_currencyrate.getRate());
					
					l_glmgr.createGL(_glHeaderGeneral);
				}
				
			}
			if(l_entity.getExtraFees() !=0){
				General_COA l_generalCOA = generalCOADAO.find(l_entity.getDefaultBranchId());
				
				if(l_generalCOA == null)
					throw new DataNotFoundException("Plz fill general coa" , General_COA.class);
				
				GeneralLedger _glHeaderGeneral = new GeneralLedger();
				_glHeaderGeneral.setGlKey(l_glmgr.getGLKey());
				_glHeaderGeneral.setSlip_ID(l_entity.getReceiptNo() + "-BA");
				_glHeaderGeneral.setSlipDate(l_entity.getReceivedDate());
				_glHeaderGeneral.setSystemDate(new Date());
				_glHeaderGeneral.setStatus("JBA");
				_glHeaderGeneral.setSrNo(3);
				_glHeaderGeneral.setOffice_ID(l_entity.getDefaultBranchId());
				_glHeaderGeneral.setCoaKey(l_generalCOA.getExtraFeesAccountID());
				_glHeaderGeneral.setDepartmentKey(l_generalCOA.getExtraFessDepartmentID());
				_glHeaderGeneral.setCurrencyKey(l_entity.getReceivedCurrencyId());
				_glHeaderGeneral.setCurrencyRateKey(l_currencyrate.getCurrencyRateId());
				_glHeaderGeneral.setExchangeRate(l_currencyrate.getRate());
				_glHeaderGeneral.setSourceAmount(-l_entity.getExtraFees());
				
				if(l_currencyrate.getRoper().equals("M"))
					_glHeaderGeneral.setHomeAmount(-(l_entity.getExtraFees() * l_currencyrate.getRate()));
				else
					_glHeaderGeneral.setHomeAmount(-(l_entity.getExtraFees() / l_currencyrate.getRate()));
				
				l_glmgr.createGL(_glHeaderGeneral);
			}	
			for(int i = 0; i < l_entity.getCrByAgentDetailList().size(); i++) {
				CurrencyRate currencyrate = currencyRateDAO.getCurrencyRateFromSourcetoHome(l_entity.getReceivedCurrencyId(), l_entity.getDefaultBranchId());
				if(l_entity.getCrByAgentDetailList().get(i).getToBranchId()!=null){
					BranchCOA l_branchCOA = branchCOADAO.findByHostBranchID(l_entity.getDefaultBranchId(), l_entity.getCrByAgentDetailList().get(i).getToBranchId());
					
					if(l_branchCOA == null)
						throw new DataNotFoundException("Plz fill BranchCOA" , BranchCOA.class);
					
					GeneralLedger _gltobranch = new GeneralLedger();
					_gltobranch.setGlKey(l_glmgr.getGLKey());
					_gltobranch.setSlip_ID(l_entity.getReceiptNo() + "-BA");
					_gltobranch.setSlipDate(l_entity.getReceivedDate());
					_gltobranch.setSystemDate(new Date());
					_gltobranch.setCoaKey(l_branchCOA.getCurrentAccountID());
					_gltobranch.setDepartmentKey(l_branchCOA.getDepartmentID());
					_gltobranch.setCurrencyKey(l_entity.getReceivedCurrencyId());
					_gltobranch.setCurrencyRateKey(currencyrate.getCurrencyRateId());
					_gltobranch.setExchangeRate(currencyrate.getRate());
					_gltobranch.setSourceAmount(-l_entity.getCrByAgentDetailList().get(i).getReceivedAmt());
					
					if(currencyrate.getRoper().equals("M"))
						_gltobranch.setHomeAmount(-(l_entity.getCrByAgentDetailList().get(i).getReceivedAmt() * currencyrate.getRate()));
					else
						_gltobranch.setHomeAmount(-(l_entity.getCrByAgentDetailList().get(i).getReceivedAmt() / currencyrate.getRate()));
					
					_gltobranch.setStatus("JBA");
					_gltobranch.setSrNo((i + 4));
					_gltobranch.setOffice_ID(l_entity.getDefaultBranchId());
					
					l_glmgr.createGL(_gltobranch);
				}
				else if(l_entity.getCrByAgentDetailList().get(i).getToAgentId()!=0){
					Agent l_agent = agentDAO.findById(l_entity.getCrByAgentDetailList().get(i).getToAgentId());
					if(l_agent != null){
						AgentCOA agentcoa = agentCOADAO.find(l_agent.getAccountset_id());
						if(agentcoa !=null){
							GeneralLedger _glagentDetail = new GeneralLedger();
							_glagentDetail.setGlKey(l_glmgr.getGLKey());
							_glagentDetail.setSlip_ID(l_entity.getReceiptNo() + "-BA");
							_glagentDetail.setSlipDate(l_entity.getReceivedDate());
							_glagentDetail.setSystemDate(new Date());
							_glagentDetail.setStatus("JBA");
							_glagentDetail.setSrNo(i + 4);
							_glagentDetail.setOffice_ID(l_entity.getDefaultBranchId());
							_glagentDetail.setCoaKey(agentcoa.getAccpayable());
							_glagentDetail.setDepartmentKey(agentcoa.getAccpayable_dept());
							_glagentDetail.setCurrencyKey(l_entity.getReceivedCurrencyId());
							_glagentDetail.setCurrencyRateKey(currencyrate.getCurrencyRateId());
							_glagentDetail.setExchangeRate(currencyrate.getRate());
							_glagentDetail.setSourceAmount(-l_entity.getCrByAgentDetailList().get(i).getReceivedAmt());
							
							if(currencyrate.getRoper().equals("M"))
								_glagentDetail.setHomeAmount(-(l_entity.getCrByAgentDetailList().get(i).getReceivedAmt() * currencyrate.getRate()));
							else
								_glagentDetail.setHomeAmount(-(l_entity.getCrByAgentDetailList().get(i).getReceivedAmt() / currencyrate.getRate()));
							
							l_glmgr.createGL(_glagentDetail);
						}else
							throw new DataNotFoundException("Agent COA doesn't exist", AgentCOA.class);
					}
					else
						throw new DataNotFoundException("Agent doesn't exist", Agent.class);
				}
		}
			l_glmgr.createGL(_glHeader);
	}
  }
	else{ //Cash Received By Individual To Pay Agent
		GeneralLedgerMgr l_glmgr = new GeneralLedgerMgr(this.sessionId);
		l_glmgr.deleteGL(l_entity.getReceiptNo() + "-IA");
		if(status == GL_STATUS.CREATE) {
			GeneralLedger _glHeader = new GeneralLedger();
			_glHeader.setGlKey(l_glmgr.getGLKey());
			_glHeader.setSlip_ID(l_entity.getReceiptNo() + "-IA");
			_glHeader.setSlipDate(l_entity.getReceivedDate());
			_glHeader.setSystemDate(new Date());
			_glHeader.setStatus("JH");
			_glHeader.setSrNo(1);
			_glHeader.setOffice_ID(l_entity.getDefaultBranchId());
			
		CurrencyRate l_currencyrate = currencyRateDAO.getCurrencyRateFromSourcetoHome(l_entity.getReceivedCurrencyId(), l_entity.getDefaultBranchId());
		 if(l_entity.getBankId()!=null){ //Bank Cheque  A/C
			Bank l_bank = bankDAO.find(l_entity.getBankId());
			if(l_bank != null){
				GeneralLedger _glHeaderBank = new GeneralLedger();
				_glHeaderBank.setGlKey(l_glmgr.getGLKey());
				_glHeaderBank.setSlip_ID(l_entity.getReceiptNo() + "-IA");
				_glHeaderBank.setSlipDate(l_entity.getReceivedDate());
				_glHeaderBank.setSystemDate(new Date());
				_glHeaderBank.setStatus("JIA");
				_glHeaderBank.setSrNo(1);
				_glHeaderBank.setOffice_ID(l_entity.getDefaultBranchId());
				_glHeaderBank.setCoaKey(l_bank.getCoaid());
				_glHeaderBank.setDepartmentKey(l_bank.getDepartmentid());
				_glHeaderBank.setCurrencyKey(l_entity.getReceivedCurrencyId());
				_glHeaderBank.setCurrencyRateKey(l_currencyrate.getCurrencyRateId());
				_glHeaderBank.setExchangeRate(l_currencyrate.getRate());
				_glHeaderBank.setSourceAmount(l_entity.getTotalReceivedAmt()+l_entity.getExtraFees());
				
				if(l_currencyrate.getRoper().equals("M"))
					_glHeaderBank.setHomeAmount(((l_entity.getTotalReceivedAmt()+l_entity.getExtraFees()) * l_currencyrate.getRate()));
				else
					_glHeaderBank.setHomeAmount(((l_entity.getTotalReceivedAmt()+l_entity.getExtraFees()) / l_currencyrate.getRate()));
				
				l_glmgr.createGL(_glHeaderBank);
			}
		  }
		 /*else{ // Branch COA A/C
					BranchCOA l_branchCOA = branchCOADAO.findByHostBranchID(l_entity.getDefaultBranchId(), l_entity.getHoBranchId());
					
					if(l_branchCOA == null)
						throw new DataNotFoundException("Plz fill BranchCOA" , BranchCOA.class);
					
					GeneralLedger _gltobranch = new GeneralLedger();
					_gltobranch.setGlKey(l_glmgr.getGLKey());
					_gltobranch.setSlip_ID(l_entity.getReceiptNo() + "-IA");
					_gltobranch.setSlipDate(l_entity.getReceivedDate());
					_gltobranch.setSystemDate(new Date());
					_gltobranch.setCoaKey(l_branchCOA.getCurrentAccountID());
					_gltobranch.setDepartmentKey(l_branchCOA.getDepartmentID());
					_gltobranch.setCurrencyKey(l_entity.getReceivedCurrencyId());
					_gltobranch.setCurrencyRateKey(l_currencyrate.getCurrencyRateId());
					_gltobranch.setExchangeRate(l_currencyrate.getRate());
					_gltobranch.setSourceAmount(l_entity.getTotalReceivedAmt()+l_entity.getExtraFees());
					
					if(l_currencyrate.getRoper().equals("M"))
						_gltobranch.setHomeAmount(((l_entity.getTotalReceivedAmt()+l_entity.getExtraFees()) * l_currencyrate.getRate()));
					else
						_gltobranch.setHomeAmount(((l_entity.getTotalReceivedAmt()+l_entity.getExtraFees()) / l_currencyrate.getRate()));
					
					_gltobranch.setStatus("JIA");
					_gltobranch.setSrNo(1);
					_gltobranch.setOffice_ID(l_entity.getDefaultBranchId());
					
					l_glmgr.createGL(_gltobranch);
				}*/
			 else{ //Cash A/C in General COA
					General_COA l_generalCOA = generalCOADAO.find(l_entity.getDefaultBranchId());
					
					if(l_generalCOA == null)
						throw new DataNotFoundException("Plz fill general coa" , General_COA.class);
					
					GeneralLedger _glCash = new GeneralLedger();
					_glCash.setGlKey(l_glmgr.getGLKey());
					_glCash.setSlip_ID(l_entity.getReceiptNo() + "-IA");
					_glCash.setSlipDate(l_entity.getReceivedDate());
					_glCash.setSystemDate(new Date());
					_glCash.setStatus("JBA");
					_glCash.setSrNo(2);
					_glCash.setOffice_ID(l_entity.getDefaultBranchId());
					_glCash.setCoaKey(l_generalCOA.getCashAccountID());
					_glCash.setDepartmentKey(l_generalCOA.getCashDepartmentID());
					_glCash.setCurrencyKey(l_entity.getReceivedCurrencyId());
					_glCash.setCurrencyRateKey(l_currencyrate.getCurrencyRateId());
					_glCash.setExchangeRate(l_currencyrate.getRate());
					_glCash.setSourceAmount(l_entity.getTotalReceivedAmt()+l_entity.getExtraFees());
					
					if(l_currencyrate.getRoper().equals("M"))
						_glCash.setHomeAmount(((l_entity.getTotalReceivedAmt()+l_entity.getExtraFees()) * l_currencyrate.getRate()));
					else
						_glCash.setHomeAmount(((l_entity.getTotalReceivedAmt()+l_entity.getExtraFees()) * l_currencyrate.getRate()));
					
					l_glmgr.createGL(_glCash);
				
				}
		 for(int i = 0; i < l_entity.getCrByAgentDetailList().size(); i++) {
     		 if(l_entity.getCrByAgentDetailList().get(i).getToAgentId() !=0){
				Agent agent = agentDAO.findById(l_entity.getCrByAgentDetailList().get(i).getToAgentId());
				if(agent != null){
					AgentCOA agentcoa = agentCOADAO.find(agent.getAccountset_id());
					if(agentcoa !=null){
						GeneralLedger _glagentHeader = new GeneralLedger();
						_glagentHeader.setGlKey(l_glmgr.getGLKey());
						_glagentHeader.setSlip_ID(l_entity.getReceiptNo() + "-IA");
						_glagentHeader.setSlipDate(l_entity.getReceivedDate());
						_glagentHeader.setSystemDate(new Date());
						_glagentHeader.setStatus("JIA");
						_glagentHeader.setSrNo(2);
						_glagentHeader.setOffice_ID(l_entity.getDefaultBranchId());
						_glagentHeader.setCoaKey(agentcoa.getAccpayable());
						_glagentHeader.setDepartmentKey(agentcoa.getAccpayable_dept());
						_glagentHeader.setCurrencyKey(l_entity.getReceivedCurrencyId());
						_glagentHeader.setCurrencyRateKey(l_currencyrate.getCurrencyRateId());
						_glagentHeader.setExchangeRate(l_currencyrate.getRate());
						_glagentHeader.setSourceAmount(-l_entity.getTotalReceivedAmt());
						
						if(l_currencyrate.getRoper().equals("M"))
							_glagentHeader.setHomeAmount(-(l_entity.getTotalReceivedAmt() * l_currencyrate.getRate()));
						else
							_glagentHeader.setHomeAmount(-(l_entity.getTotalReceivedAmt() / l_currencyrate.getRate()));
						
						
						l_glmgr.createGL(_glagentHeader);
						
					}else
						throw new DataNotFoundException("Agent COA doesn't exist", AgentCOA.class);
					
				}
				else
					throw new DataNotFoundException("Agent doesn't exist", Agent.class);
			}
		  }
		 if(l_entity.getExtraFees() !=0){
			 General_COA l_generalCOA = generalCOADAO.find(l_entity.getDefaultBranchId());
				
				if(l_generalCOA == null)
					throw new DataNotFoundException("Plz fill general coa" , General_COA.class);
				
				GeneralLedger _glHeaderGeneral = new GeneralLedger();
				_glHeaderGeneral.setGlKey(l_glmgr.getGLKey());
				_glHeaderGeneral.setSlip_ID(l_entity.getReceiptNo() + "-IA");
				_glHeaderGeneral.setSlipDate(l_entity.getReceivedDate());
				_glHeaderGeneral.setSystemDate(new Date());
				_glHeaderGeneral.setStatus("JIA");
				_glHeaderGeneral.setSrNo(3);
				_glHeaderGeneral.setOffice_ID(l_entity.getDefaultBranchId());
				_glHeaderGeneral.setCoaKey(l_generalCOA.getExtraFeesAccountID());
				_glHeaderGeneral.setDepartmentKey(l_generalCOA.getExtraFessDepartmentID());
				_glHeaderGeneral.setCurrencyKey(l_entity.getReceivedCurrencyId());
				_glHeaderGeneral.setCurrencyRateKey(l_currencyrate.getCurrencyRateId());
				_glHeaderGeneral.setExchangeRate(l_currencyrate.getRate());
				_glHeaderGeneral.setSourceAmount(-l_entity.getExtraFees());
				
				if(l_currencyrate.getRoper().equals("M"))
					_glHeaderGeneral.setHomeAmount(-(l_entity.getExtraFees() * l_currencyrate.getRate()));
				else
					_glHeaderGeneral.setHomeAmount(-(l_entity.getExtraFees() / l_currencyrate.getRate()));
				
				l_glmgr.createGL(_glHeaderGeneral);
			
		 }
		 l_glmgr.createGL(_glHeader);
		}
	  }
	}
 }

